package com.example.bookstore.beans;

import java.util.Date;

import javax.faces.bean.ManagedBean;
import javax.faces.component.UIOutput;
import javax.faces.component.UISelectBoolean;

import com.example.bookstore.converters.CreditCardConverter;

@ManagedBean(name = "cashier")
public class CheckoutBean extends BaseBean {

	protected Date shipDate;

	// ---------------------------------------------------- Component Properties
	protected String name = null;
	protected String shippingOption = "2";
	protected String[] newsletters = new String[0];
	UIOutput specialOfferText = null;
	UIOutput thankYou = null;
	UISelectBoolean specialOffer = null;
	private CreditCardConverter creditCard = null;
	private String stringProperty = "This is a String property";

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return this.name;
	}

	public CreditCardConverter getCreditCard() {
		if (creditCard == null) {
			return new CreditCardConverter();
		}

		return creditCard;
	}

	public void setCreditCard(CreditCardConverter newCreditCard) {
		this.creditCard = newCreditCard;
	}

	public void setNewsletters(String[] newsletters) {
		this.newsletters = newsletters;
	}

	public String[] getNewsletters() {
		return this.newsletters;
	}

	public Date getShipDate() {
		return this.shipDate;
	}

	public void setShipDate(Date shipDate) {
		this.shipDate = shipDate;
	}

	public void setShippingOption(String shippingOption) {
		this.shippingOption = shippingOption;
	}

	public String getShippingOption() {
		return this.shippingOption;
	}

	public UIOutput getSpecialOfferText() {
		return this.specialOfferText;
	}

	public void setSpecialOfferText(UIOutput specialOfferText) {
		this.specialOfferText = specialOfferText;
	}

	public UISelectBoolean getSpecialOffer() {
		return this.specialOffer;
	}

	public void setSpecialOffer(UISelectBoolean specialOffer) {
		this.specialOffer = specialOffer;
	}

	public UIOutput getThankYou() {
		return this.thankYou;
	}

	public void setThankYou(UIOutput thankYou) {
		this.thankYou = thankYou;
	}

	public String getStringProperty() {
		return (this.stringProperty);
	}

	public void setStringProperty(String stringProperty) {
		this.stringProperty = stringProperty;
	}

	// ----------------------------------------------------- Application Actions
	public String submit() {
		System.out.println("CheckoutBean: submit");
		// Subscribing to newsletters would go here

		// Calculate and save the ship date
		int days = Integer.valueOf(shippingOption).intValue();
		Date shipDate = new Date();
		shipDate = new Date(shipDate.getTime()
				+ ((long) days * (long) 86400000));
		setShipDate(shipDate);

		try {
			
			bookController().buyBooks(shoppingCart(), null);
		} catch (Exception ex) {
			return "bookordererror";
		}

		clear();

		return "receipt";

	}
}
